package com.island.practice.servlet;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Iterator;
import java.io.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

import com.island.practice.entity.Customer;
//import com.island.practice.servlet.JPAUtil;


public class ListCustomerServlet extends HttpServlet{
	
	@PersistenceUnit(unitName="A")
    private EntityManagerFactory emFactory;
	//private UserTransaction ut;
	
	protected void processRequest(HttpServletRequest request, 
            HttpServletResponse response)
                throws ServletException, IOException {
		
		
		EntityManager entityManager = null;
		
		try {
            	//entityManager = emFactory.createEntityManager();
            	//entityManager = JPAUtil.getEntityManagerFactory().createEntityManager();
				
				//emFactory = Persistence.createEntityManagerFactory("A");
				entityManager = emFactory.createEntityManager();
			
            	Query query = entityManager.createQuery("SELECT * FROM CUSTOMER");
            
            	Iterator customers = query.getResultList().iterator();
    	        while(customers.hasNext()) {
    	            //Customer customer = (Customer) customers.next();
    	            //System.out.printf("%d\t%s\t%d\n",
    	            //        customer.getId(),
    	            //        customer.getName(),
    	            //        customer.getAddress());
    	        }

        } catch (Exception ex) {
            Logger.getLogger(Customer.class.getName())
                    .log(Level.SEVERE, null, ex);
        } finally { 
            entityManager.close();
           
            
        }
		
		
		
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
		    
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException, IOException {
		processRequest(request, response);
	}
	
}
